<?php

class Bet_Model_NewsArticleMapper extends Bet_Model_Mapper {

	public function __construct() {
		$this->setDbTableName("Bet_Model_DbTable_NewsArticle");
		$this->setId('news_article_id');
	}

	/**
	 * Retrieve an instance from the datastore by id
	 * @param $newsArticleId
	 * @return Bet_Model_NewsArticle
	 */
	public function findById($newsArticleId) {
		$data = $this->findByIdToArray($newsArticleId);
		return ($data != null ? new Bet_Model_NewsArticle($data) : null);
	}

	/**
	 * Retrieve an instance's data from the datastore by id
	 * @param $newsArticleId
	 * @return array Data fetched from the instance as an array
	 */
	public function findByIdToArray($newsArticleId) {
		$result = $this->findToArray($newsArticleId);
		return count($result) > 0 ? $result[0] : null;
	}

	/**
	 * Save or update an model object of a db instance
	 * @param Bet_Model_Group $group
	 * @return int | boolean Saving result - new row's id if save new object
	 * 		| TRUE if successed, otherwise FALSE if update an existing object
	 */
	public function save(Bet_Model_NewsArticle $newsArticle, $isUpdate = false) {
		$data = array(
			"title" => $newsArticle->getTitle(),
			"text" => $newsArticle->getText(),
			"news_category_id" => $newsArticle->getNewsCategoryId(),
		);

		if ($isUpdate) {
			$data["news_article_id"]  = $newsArticle->getNewsArticleId();
		} else {
			$identity = Zend_Auth::getInstance()->getIdentity();
			$data["created_by"] = $identity->user_id;
			$data["news_article_id"]  = null;
			$data["status"]  = Bet_Model_NewsArticle::STATUS_ENABLE;
		}

		return parent::save($data);
	}

}

